﻿
@{
    ViewBag.Title = "登录名";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="row">
    <div  class="btn-group" role="group" style="margin-top: 10px;">
        <a href="javascript:tabChange(0);" class="btn btn-default btn-sm"> 登录</a>
        <a href="javascript:tabChange(1);" class="btn btn-default btn-sm"> 注册 </a>
    </div>
    <form class="from" autocomplete="on">
        <div class="form-group">
            <label class="control-label" for="n">登录名</label>
            <input type="text" value="" placeholder="" id="n" class="form-control" required="required" />
        </div>
        <div class="form-group">
            <label class="control-label" for="p1">密&nbsp;&nbsp;码</label>
            <input type="password" value="" id="p1" class="form-control" required="required" />
        </div>
        <div class="form-group">
            <label class="control-label" for="p2">确认密码</label>
            <input type="password" value="" id="p2" class="form-control" required="required" />
        </div>
        <div class="form-group">
            <input type="button" value="注册" class="btn btn-primary" id="btnReg" />

            <input type="reset" value="重置" class="btn btn-default" style="margin-left: 100px" />
        </div>
    </form>
    <form class="from" autocomplete="on" id="regfrom" style="display: none">
        <div class="form-group">
            <label class="control-label" for="loginname">登录名</label>
            <input type="text" value="" placeholder="" id="loginname" class="form-control" required="required" />
        </div>
        <div class="form-group">
            <label class="control-label" for="loginpwd">密&nbsp;&nbsp;码</label>
            <input type="password" value="" id="loginpwd" class="form-control" required="required" />
        </div>
        <div class="form-group">
            <input type="button" value="登录" class="btn btn-primary" id="btnLogin" />

            <input type="reset" value="重置" class="btn btn-default" style="margin-left: 100px" />
        </div>
    </form>

    <div id="tip">

    </div>
</div>


@section scripts
{
    <script>
        $(function() {
            
            //登录
            $("#btnLogin")
                .click(function() {
                    //var f = $("form:first");
                    var n = $("#loginname").val();
                    var p1 = $("#loginpwd").val();
                    if (!n) {
                        $("#loginname").focus();
                        alert("登录名不可以为空");
                        return;
                    }
                    if (!p1) {
                        $("#loginpwd").focus();
                        alert("密码不可以为空");
                        return;
                    }
                    $.post('/account/login',
                        { Name: n, Pwd: p1 },
                        function(res) {
                            if (res.Code === 0) {
                                $("#tip").html("<div class='alert alert-success'  role='alert'>登录成</div>");
                                location.href = '/info/index';
                            } else {
                                $("#tip").html("<div class='alert alert-danger' role='alert'>" + res.Msg + "</div>");
                            }
                        });

                });

            //注册
            $("#btnReg")
                .click(function() {
                    //var f = $("form:first");
                    var n = $("#n").val();
                    var p1 = $("#p1").val();
                    var p2 = $("#p2").val();
                    if (!n) {
                        $("#n").focus();
                        alert("登录名不可以为空");
                        return;
                    }
                    if (!p1) {
                        $("#p1").focus();
                        alert("密码不可以为空");
                        return;
                    }
                    if (!p2) {
                        $("#p2").focus();
                        alert("确认密码不可以为空");
                        return;
                    }

                    if (p1 != p2) {
                        alert("两次密码输入不一致");
                        return;
                    }

                    $.post('/account/regist',
                        { Name: n, Pwd: p1 },
                        function(res) {
                            if (res.Code === 0) {
                                $("#tip").html("<span>注册成功</span>");
                                location.href = '/info/index';
                            } else {
                                $("#tip").html("<span>注册失败</span><span>" + res.Msg + "</span>");
                            }
                        });

                });

        });

        function tabChange(i) {
            $("form").hide();
            $("form:eq(" + i + ")").show();
        }
    </script>

}
